<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="700"
    destroy-on-close
    append-to-body
    @closed="emits('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="80px"
      :style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
    >

      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="form.name" clearable placeholder="请输入仓库名称" />
      </el-form-item>

      <el-form-item label="仓库地址" prop="address">
        <el-input v-model="form.address" clearable placeholder="请输入仓库地址" />
      </el-form-item>

      <el-form-item label="仓储费" prop="storage_fee">
        <el-input v-model="form.storage_fee" clearable placeholder="请输入仓储费" />
      </el-form-item>

      <el-form-item label="搬运费" prop="carry_fee">
        <el-input v-model="form.carry_fee" clearable placeholder="请输入搬运费" />
      </el-form-item>

      <el-form-item label="负责人" prop="user_id">
        <ma-select-user v-model="form.user_id" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" :active-value="proxy.$CONFIG.SWITCH.ENABLE" :inactive-value="proxy.$CONFIG.SWITCH.DISABLE" />
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" clearable placeholder="请输入排序" />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
  import { ref, reactive, defineExpose, onMounted,getCurrentInstance } from 'vue'
  import {useRouter} from "vue-router";
  import { ElMessage } from 'element-plus'
  import erpStorehouse from '@/api/apis/erp/erpStorehouse'
  import systemDict from '@/api/apis/system/dataDict'


  const emits = defineEmits(['success', 'closed'])
  const { proxy } = getCurrentInstance();
  const router = useRouter();

  const mode = ref('add')
  const treeList = ref([])
  const visible = ref(false)
  const isSaveing = ref(false)
  const dialogForm = ref(null)

  const titleMap = reactive({ add: '新增仓库信息', edit: '编辑仓库信息' })
  const dictData = reactive({

  })
  const form = reactive({
    id:'0',
name:'',
address:'',
storage_fee:'0',
carry_fee:'0',
user_id:'0',
status:proxy.$CONFIG.SWITCH.ENABLE,
sort:'0',
mer_id:'0',

  })
  const rules = reactive({

  })

  onMounted(async () => {
    await getDictData()
    if(router.currentRoute.value.query.open) {
    	open('add');
    }
  })

  const getDictData = () => {

  }

  const open = (type = 'add') => {
    mode.value = type
    visible.value = true

  }

  const submit = () => {
    dialogForm.value.validate(async (valid) => {
      if (valid) {
        isSaveing.value = true
        const res = mode.value === 'add' ? await erpStorehouse.save(form) : await erpStorehouse.update(form.id, form)
        if (res.success) {
          emits('success', form, mode.value)
          visible.value = false
          ElMessage.success(res.message)
        } else {
          ElMessage.error(res.message)
        }
        isSaveing.value = false
      }
    })
  }

  const setData = (data) => {
    for (let k in form) {
      if (data[k] || data[k] === 0) {
        form[k] = data[k]
      }
    }
  }


  defineExpose({
    open, setData
  })
</script>
